/*
   Copyright (c) 2016-2017 Slamtec Co., Ltd. All Rights Reserved.
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
       http://www.apache.org/licenses/LICENSE-2.0
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.csjbot.snowbot_rogue.servers.slams.agent;


import java.util.ArrayList;

class Worker {
    private final static String TAG = "worker";

    private final ArrayList<Runnable> jobQueue = new ArrayList<>();

    private final Object WorkerLock = new Object();
    private Thread worker;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            ArrayList<Runnable> jobs = new ArrayList<>();

            while (true) {
                synchronized (jobQueue) {
                    jobs.addAll(jobQueue);
                    jobQueue.clear();
                }

                if (jobs.isEmpty()) {
                    break;
                }

                for (Runnable job : jobs) {
//                    Csjlogger.debug("job is {}", job);
                    // Logger.d("chenqi " + job);
                    job.run();
                }
                jobs.clear();
            }

            synchronized (StateLock) {
                state = IDLE;
            }
        }
    };

    private final static int IDLE = 0;
    private final static int WORKING = 1;

    private int state;
    private final Object StateLock = new Object();

    Worker() {
        state = IDLE;
        worker = new Thread(runnable);
    }

    void push(Runnable job) {
        synchronized (jobQueue) {
            jobQueue.add(job);
        }

        execute();
    }

    void pushHead(Runnable job) {
        synchronized (jobQueue) {
            jobQueue.add(0, job);
        }

        execute();
    }

    void clear() {
        synchronized (jobQueue) {
            jobQueue.clear();
        }
    }

    private void execute() {
        synchronized (StateLock) {
            if (state == WORKING) {
                return;
            }
            state = WORKING;
        }

        if (worker.getState() == Thread.State.TERMINATED) {
            worker = new Thread(runnable);
        }

        if (worker.getState() == Thread.State.WAITING) {
            synchronized (WorkerLock) {
                worker.notify();
            }
        }

        if (worker.getState() == Thread.State.NEW) {
            worker.start();
        }

//        Csjlogger.debug("worker.getState() {}", worker.getState());
    }
}
